//
//  MKAPIHttpFile.m
//  MKAPI
//
//  Created by 苏尚进 on 2022/9/6.
//  Copyright © 2022 moon. All rights reserved.
//

#import "MKAPIHttpFile.h"
#import "MKAPIPreCompile.h"
#import "NSObject+MKAPI.h"

@implementation MKAPIHttpFile
+ (MKAPIHttpFile *)fileWithName:(NSString *)name filePath:(NSString *)filePath{
    MKAPIHttpFile *file = [[self alloc] init];
    file.name = name;
    file.fileName = filePath.lastPathComponent;
    file.filePath = filePath;
    file.mimeType = filePath.pathExtension.mkapi_contentTypeForPathExtension;
    return file;
}
+ (MKAPIHttpFile *)fileWithName:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType data:(NSData *)data{
    MKAPIHttpFile *file = [[self alloc] init];
    file.name = name;
    file.fileName = fileName;
    file.mimeType = mimeType;
    file.data = data;
    return file;
}
- (NSString *)description{
    NSString *desc = [NSString stringWithFormat:@"<%@:%p,name=%@,fileName=%@,mimeType=%@,filePath=%@>",NSStringFromClass(self.class),self,self.name,self.fileName,self.mimeType,self.filePath];
    return desc;
}
@end


@interface MKAPIHttpFilesParam(){
    NSMutableArray<MKAPIHttpFile *> *_files;
}
@property(nonatomic,strong) MKAPIHttpFilesParam *(^P)(MKAPIHttpFile *file);
@end
@implementation MKAPIHttpFilesParam
- (id)init{
    if (self=[super init]) {
        _files = [[NSMutableArray alloc] init];
        @MKAPI_WEAKIFY(self);
        [self setP:^MKAPIHttpFilesParam *(MKAPIHttpFile *file) {
            @MKAPI_NORMALIZE(self);
            if(file){
                [self->_files addObject:file];
            }
            return self;
        }];
    }
    return self;
}
- (NSString *)description{
    NSString *desc = [NSString stringWithFormat:@"<%@:%p,files=%@>",NSStringFromClass(self.class),self,self.files];
    return desc;
}
- (void)removeAll{
    [_files removeAllObjects];
}
- (void)removeFile:(MKAPIHttpFile *)file{
    [_files removeObject:file];
}
@end
